package com.agileai.pm.module.proc.exteral;

import java.util.ArrayList;
import java.util.List;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.domain.TreeBuilder;
import com.agileai.hotweb.domain.TreeModel;
import com.agileai.hotweb.ws.BaseRestService;
import com.agileai.pm.cxmodule.ProcPlanAdminManage;

public class ProjPlanImpl extends BaseRestService implements ProjPlan{

	@Override
	public String findPlanInfos(String projectId) {
		ProcPlanAdminManage procPlanAdminManage= (ProcPlanAdminManage) this.lookupService(ProcPlanAdminManage.class);
		DataParam param=new DataParam();
		param.put("projectId", projectId);
		List<DataRow>rsList=procPlanAdminManage.findMobileRecords(param);
		TreeBuilder treeBuilder = new TreeBuilder(rsList, "PPA_ID", "PPA_NAME", "PPA_PID");
		treeBuilder.setRootId(projectId);
		TreeModel treeModel = treeBuilder.buildTreeModel();
		List<JSONObject>list=new ArrayList<JSONObject>();
		
		try {
        	JSONArray jsonArray = new JSONArray();
        	List<TreeModel> topChildren = treeModel.getChildren();
        	for (int i=0;i < topChildren.size();i++){
        		JSONObject jsonObject = new JSONObject();
        		TreeModel child = topChildren.get(i);
        		jsonObject.put("ppaId", child.getId());
        		jsonObject.put("ppaName", child.getName());
        		jsonObject.put("ppaType", child.getProperty().get("PPA_TYPE"));
        		jsonObject.put("ppaContent", child.getProperty().get("PPA_CONTENT"));
        		jsonObject.put("ppaCharge", child.getProperty().get("PPA_CHARGE"));
        		jsonObject.put("ppaWorkDay", child.getProperty().get("PPA_WORK_DAY"));
        		jsonObject.put("ppaStartTime", child.getProperty().get("PPA_START_TIME"));
        		jsonObject.put("ppaState", child.getProperty().get("PPA_STATE_NAME"));
        		jsonObject.put("ppaEndTime", child.getProperty().get("PPA_END_TIME"));
        		jsonObject.put("ppaCreateName", child.getProperty().get("PPA_CREATE_NAME"));
        		jsonObject.put("serialNumber", i+1);
        		jsonObject.put("level", 1);
        		list.add(jsonObject);
        		if (child.getChildren().size() > 0){
        			JSONArray jsonArrayChild = new JSONArray();
        			this.loopBuildJson(list,child, jsonArrayChild,i+1+"");
        			jsonObject.put("children",jsonArrayChild);
        		}
        		jsonArray.put(jsonObject);
        	}	
        	jsonArray.toString();
		} catch (Exception e) {
			log.error(e.getLocalizedMessage(),e);
		}
		String jsonString=toJsonString(list);
		return jsonString;
	} 
	
	 private void loopBuildJson(List<JSONObject>list,TreeModel treeModel,JSONArray jsonArray,String serialNumber) throws JSONException{
	    	List<TreeModel> topChildren = treeModel.getChildren();
	    	for (int i=0;i < topChildren.size();i++){
	    		JSONObject jsonObject = new JSONObject();
	    		TreeModel child = topChildren.get(i);
	    		jsonObject.put("ppaId", child.getId());
	    		jsonObject.put("ppaName", child.getName());
	    		jsonObject.put("ppaType", child.getProperty().get("PPA_TYPE"));
	    		jsonObject.put("ppaContent", child.getProperty().get("PPA_CONTENT"));
	    		jsonObject.put("ppaCharge", child.getProperty().get("PPA_CHARGE"));
	    		jsonObject.put("ppaWorkDay", child.getProperty().get("PPA_WORK_DAY"));
	    		jsonObject.put("ppaStartTime", child.getProperty().get("PPA_START_TIME"));
	    		jsonObject.put("ppaState", child.getProperty().get("PPA_STATE_NAME"));
	    		jsonObject.put("ppaEndTime", child.getProperty().get("PPA_END_TIME"));
	    		jsonObject.put("ppaCreateTime", child.getProperty().get("PPA_CREATE_TIME"));
	    		jsonObject.put("ppaCreateName", child.getProperty().get("PPA_CREATE_NAME"));
	    		jsonObject.put("serialNumber", serialNumber+"."+(i+1));
	    		jsonObject.put("level", getlevel(serialNumber+"."+(i+1)));
	    		list.add(jsonObject);
	    		if (child.getChildren().size() > 0){
	    			JSONArray jsonArrayChild = new JSONArray();
	    			this.loopBuildJson(list,child, jsonArrayChild,serialNumber+"."+(i+1));
	    			jsonObject.put("children",jsonArrayChild);
	    		}
	    		jsonArray.put(jsonObject);
	    	}
	    }
	 
	public String toJsonString(List<JSONObject>list){
		JSONArray jsonArry = new JSONArray();
		for(JSONObject jsonObject:list){
			jsonArry.put(jsonObject);
		}
		JSONObject jsonObject = new JSONObject();
		try {
			jsonObject.put("projectPlan", jsonArry);
		} catch (JSONException e) {
		}
		return jsonObject.toString();
	}
	
	public int getlevel(String str){
		  int count=0;
		  for(int i=0;i<str.length();i++){
			   if(str.charAt(i)=='.'){
			   count++;
			   }
		  }
		  return count+1;
	}
}
